const express = require('express');
const connection = require('../mysql') // 获取连接实例
const app = express();
const $sql = require('../db');

//获取所有供应商信息
app.get('/getsupalldata',(req,res) => {
	var sql = $sql.supplie_sql.sup_getalldata;
	connection.query(sql,(err,result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//添加供应商信息
app.post('/supadd',(req,res) => {
	var sql = $sql.supplie_sql.inst_supdata;
	var params = [req.body.name,req.body.linl_name,req.body.tel,req.body.address,req.body.email,
					req.body.company_location,req.body.bank_name,req.body.bank_account];
	connection.query(sql,params,(err,result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.json({code:200,message:'添加成功'});
		}
	})
})
//更新指定供应商信息
app.put('/upsupdata',(req,res) => {
	var sql = $sql.supplie_sql.sup_upsupdata;
	var params = [req.body.linl_name,req.body.tel,req.body.address,req.body.email,
					req.body.bank_name,req.body.bank_account,req.body.company_location,req.body.id]			
	connection.query(sql,params,(err,result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//删除供应商
app.delete('/supdelect',(req,res) => {
	var sql = $sql.supplie_sql.sup_delect;
	var params = [req.body.id];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//获取供应商的id和name
app.get('/sup_getdata', (req, res) => {
	var sql = $sql.supplie_sql.sup_getdata;
	connection.query(sql,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})

module.exports = app;